﻿using System;
using System.Collections.Generic;
using System.Text;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Validation.Interfaces;

namespace Kiiro.Validation.Validators.Entities
{
    public class WorkValidator : BaseEntityValidator, IValidator<Work>
    {
        private readonly List<ValidationMessage> validationErrors = new List<ValidationMessage> ( );

        #region  IValidator<Work> Members
        public List<ValidationMessage> Validate ( Work entityToValidate)
        {
            // If the entity is marked as deleted, don't do any validation, just return.
            if ( entityToValidate.EditType != EditType.Deleted )
            {
                ValidationMessage message = null;

                #region Required Fields Checks

                message = IsRequired ( entityToValidate.Title, "Title" );

                if ( message != null )
                {
                    entityToValidate.TitleMessage.Add ( message );
                    validationErrors.Add ( message );
                }

                if ( entityToValidate.WorkItemType == WorkType.Issue )
                {

                    message = IsRequired ( entityToValidate.Status.ToString ( ), "Status" );

                    if ( message != null )
                    {
                        entityToValidate.StatusMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                }
                #endregion

                #region Comparison Checks

                if ( entityToValidate.StartDate != DateTime.MinValue && entityToValidate.EndDate != DateTime.MinValue )
                {
                    message = NotLessThan ( entityToValidate.EndDate, entityToValidate.StartDate, "Start Date", "End Date" );
                    if ( message != null )
                    {
                        entityToValidate.StartDateMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( entityToValidate.WorkItemType == WorkType.Task )
                {
                    message = NotBetween ( entityToValidate.PercentageComplete, "Percentage Complete" );
                    if ( message != null )
                    {
                        entityToValidate.PercentageCompleteMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                //message = NotNegative ( entityToValidate.AssignedWork, "Estimated Time" );
                //if ( message != null )
                //{
                //    entityToValidate.AssignedWorkMessage.Add ( message );
                //    validationErrors.Add ( message );
                //}

                //if ( entityToValidate.AssignedWork < 0.1 )
                {
                    object temp = Common.Utility.CacheHelper.GetPortalCache ( Constants.CacheKeys.Setup, entityToValidate.SiteCollectionId, entityToValidate.ServerRelativeUrl );

                    if ( temp != null )
                    {
                        Setup setup = ( Setup ) temp;
                        // maxValue =  (MinutesPerHour * HoursPerDay * DaysPerMonth * MonthsInTenYears) + ( add the two days on leap years)
                        double maxValue = ( ( ( 60 * setup.HoursPerDay ) * 20 ) * 120 ) + ( ( 60 * setup.HoursPerDay ) * 2 );

                        message = NotGreaterThanTenYears ( entityToValidate.AssignedWork, "Estimated Time", maxValue, setup.HoursPerDay );
                        if ( message != null )
                        {
                            entityToValidate.AssignedWorkMessage.Add ( message );
                            validationErrors.Add ( message );
                        }
                    }
                    else
                    {
                        //5262480 minutes (10 years, counting two leap years)
                        message = NotGreaterThanTenYears ( entityToValidate.AssignedWork, "Estimated Time", 5262480, 8 );
                        if ( message != null )
                        {
                            entityToValidate.AssignedWorkMessage.Add ( message );
                            validationErrors.Add ( message );
                        }
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Title ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.Title, "Title" );

                    if ( message != null )
                    {
                        entityToValidate.TitleMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.Title, "Title" );

                    if ( message != null )
                    {
                        entityToValidate.TitleMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Description ) )
                {
                    message = CheckMaximumLength ( 1000, entityToValidate.Description, "Description" );
                    if ( message != null )
                    {
                        entityToValidate.DescriptionMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.Description, "Description" );

                    if ( message != null )
                    {
                        entityToValidate.DescriptionMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Title ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.StatusNote, "Status Note" );
                    if ( message != null )
                    {
                        entityToValidate.StatusNoteMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.StatusNote, "Status Note" );

                    if ( message != null )
                    {
                        entityToValidate.StatusNoteMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }
                #endregion


                #region Attachments Validation
                if (entityToValidate.AttachmentList != null && entityToValidate.AttachmentList.Count > 0)
                {
                    if (entityToValidate.AttachmentList.Find(x => x.EditType == EditType.Unchanged) != null)
                    {
                        foreach (Attachment file in entityToValidate.AttachmentList)
                        {
                            message = AreAttachmentsValidExtensions(entityToValidate, file);

                            if (message != null)
                            {
                                entityToValidate.AttachmentListMessage.Add(message);
                                validationErrors.Add(message);
                            }
                        }
                    }
                }
                #endregion
            }

            return validationErrors;
        }


        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Validation/Validators/Entities/WorkValidator.cs $
 * 
 * 7     12/14/09 2:10p Shafia
 * attachments file extensions validation for discussions, tasks, and
 * issues
 * 
 * 6     10/26/09 11:59a Adamw
 * Case 332
 * 
 * 5     21/10/09 3:06p Shafia
 * 
 * 4     21/10/09 11:39a Shafia
 * Validating that estimated time doesn't exceed 10 years ( 10 years time
 * is based on number of hours per day from the setup. Example: 10 years
 * based on 8 hours has more minutes than 10 years based on 4 hours per
 * day)
 * 
 * 3     17/08/09 10:45a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/